﻿using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

using LTJ.Data;
using LTJ.Common;
using LTJ.Config;
using LTJ.Entity;

namespace LTJ.Data.SqlServer
{
    public partial class DataProvider : IDataProvider
    {
        private static int _lastRemoveTimeout;

        /// <summary>
        /// 创建新用户.
        /// </summary>
        /// <param name="__userinfo">用户信息</param>
        /// <returns>返回用户ID, 如果已存在该用户名则返回-1</returns>
        public int CreateUser(UserInfo userInfo)
        {

            DbParameter[] parms = {
					DbHelper.MakeInParam("@lname", (DbType)SqlDbType.VarChar,20,userInfo.lname),
					DbHelper.MakeInParam("@rname", (DbType)SqlDbType.NVarChar,20,userInfo.rname),
					DbHelper.MakeInParam("@ename", (DbType)SqlDbType.VarChar,50,userInfo.ename),
					DbHelper.MakeInParam("@email", (DbType)SqlDbType.VarChar,50,userInfo.email),
					DbHelper.MakeInParam("@pass", (DbType)SqlDbType.VarChar,50,userInfo.pass),
					DbHelper.MakeInParam("@gid", (DbType)SqlDbType.Int,4,userInfo.gid),
					DbHelper.MakeInParam("@cid", (DbType)SqlDbType.Int,4,userInfo.cid),
					DbHelper.MakeInParam("@ip", (DbType)SqlDbType.Char,15,userInfo.ip),
					DbHelper.MakeInParam("@status", (DbType)SqlDbType.Int,4,userInfo.status),
                    DbHelper.MakeInParam("@authority", (DbType)SqlDbType.VarChar,2000,userInfo.authority),
                    DbHelper.MakeInParam("@scores", (DbType)SqlDbType.Int,4,userInfo.scores),
					DbHelper.MakeInParam("@source", (DbType)SqlDbType.SmallInt,2,userInfo.source),
					DbHelper.MakeInParam("@creater", (DbType)SqlDbType.VarChar,20,userInfo.creater),
					DbHelper.MakeInParam("@syspass", (DbType)SqlDbType.VarChar,50,userInfo.syspass),
					DbHelper.MakeInParam("@gender", (DbType)SqlDbType.Bit,1,userInfo.gender),
					DbHelper.MakeInParam("@phone", (DbType)SqlDbType.VarChar,20,userInfo.phone),
					DbHelper.MakeInParam("@bio", (DbType)SqlDbType.VarChar,2000,userInfo.bio)
		    };

            return TypeConverter.ObjectToInt(DbHelper.ExecuteScalar(CommandType.StoredProcedure, string.Format("{0}user_ADD", BaseConfigs.GetTablePrefix), parms), -1);
        }

        public IDataReader GetUserInfoByID(int id)
        {
            DbParameter[] parms = {
                                      DbHelper.MakeInParam("@id", (DbType)SqlDbType.Int,4, id)
                                  };
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, string.Format("{0}user_GetModel", BaseConfigs.GetTablePrefix), parms);
        }

        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public IDataReader GetUserInfoByName(string userName)
        {
            DbParameter[] parms = {
									   DbHelper.MakeInParam("@username",(DbType)SqlDbType.VarChar,20,userName),
			                      };
            string commandText = string.Format("SELECT TOP 1 {0} FROM [{1}user] WHERE [lname]=@username",
                                                DbFields.USERINFO,
                                                BaseConfigs.GetTablePrefix);
            return DbHelper.ExecuteReader(CommandType.Text, commandText, parms);
        }

        /// <summary>
        /// 获取用户分页数据
        /// </summary>
        public DataTable GetUsersByPage(int pageindex, int pagesize, string condition, string orderstr)
        {
            DbParameter[] parms = {
                          DbHelper.MakeInParam("@pageindex",(DbType)SqlDbType.Int,4,pageindex),
                          DbHelper.MakeInParam("@pagesize",(DbType)SqlDbType.Int,4,pagesize),
                          DbHelper.MakeInParam("@conditions",(DbType)SqlDbType.NVarChar,2000,condition),
                          DbHelper.MakeInParam("@orderstr",(DbType)SqlDbType.NVarChar,2000,orderstr)
            };
            return DbHelper.ExecuteDataset(CommandType.StoredProcedure, string.Format("{0}getuserlistbypage", BaseConfigs.GetTablePrefix), parms).Tables[0];
        }

        /// <summary>
        /// 获取用户数量
        /// </summary>
        public int GetUsersCount(string querystring)
        {
            DbParameter[] parms = { DbHelper.MakeInParam("@conditions", (DbType)SqlDbType.VarChar, 2000, querystring) };
            return TypeConverter.ObjectToInt(DbHelper.ExecuteScalar(CommandType.StoredProcedure, string.Format("{0}getusercountbycondition", BaseConfigs.GetTablePrefix), parms));
        }

        /// <summary>
        /// 编辑用户.
        /// </summary>
        public bool UpdateUser(LTJ.Entity.UserInfo userInfo)
        {
            DbParameter[] parms = {
                    DbHelper.MakeInParam("@id",(DbType)SqlDbType.Int,4,userInfo.id),
					DbHelper.MakeInParam("@lname", (DbType)SqlDbType.VarChar,20,userInfo.lname),
					DbHelper.MakeInParam("@rname", (DbType)SqlDbType.NVarChar,20,userInfo.rname),
					DbHelper.MakeInParam("@ename", (DbType)SqlDbType.VarChar,50,userInfo.ename),
					DbHelper.MakeInParam("@email", (DbType)SqlDbType.VarChar,50,userInfo.email),
					DbHelper.MakeInParam("@pass", (DbType)SqlDbType.VarChar,50,userInfo.pass),
					DbHelper.MakeInParam("@gid", (DbType)SqlDbType.Int,4,userInfo.gid),
					DbHelper.MakeInParam("@cid", (DbType)SqlDbType.Int,4,userInfo.cid),
					DbHelper.MakeInParam("@ip", (DbType)SqlDbType.Char,15,userInfo.ip),
					DbHelper.MakeInParam("@status", (DbType)SqlDbType.Int,4,userInfo.status),
                    DbHelper.MakeInParam("@authority", (DbType)SqlDbType.VarChar,2000,userInfo.authority),
                    DbHelper.MakeInParam("@scores", (DbType)SqlDbType.Int,4,userInfo.scores),
					DbHelper.MakeInParam("@source", (DbType)SqlDbType.SmallInt,2,userInfo.source),
					DbHelper.MakeInParam("@syspass", (DbType)SqlDbType.VarChar,50,userInfo.syspass),
					DbHelper.MakeInParam("@gender", (DbType)SqlDbType.Bit,1,userInfo.gender),
					DbHelper.MakeInParam("@phone", (DbType)SqlDbType.VarChar,20,userInfo.phone),
					DbHelper.MakeInParam("@bio", (DbType)SqlDbType.VarChar,2000,userInfo.bio)
		    };
            return DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, string.Format("{0}user_Update", BaseConfigs.GetTablePrefix), parms) > 0;
        }

        /// <summary>
        /// 去除用户的用户组信息
        /// </summary>
        /// <param name="ids">用户id组合</param>
        /// <param name="gid">用户组id</param>
        /// <returns></returns>
        public int RemoveUser(string ids,int gid)
        {
            ids = LTJ.Common.Utils.returnIdList(ids);
            DbParameter[] parms = {
                                      DbHelper.MakeInParam("@gid",(DbType)SqlDbType.Int,4,gid),
									   DbHelper.MakeInParam("@ids",(DbType)SqlDbType.VarChar,2000,ids)
			                      };
            string commandText = string.Format("update  [{1}user] set gid=@gid WHERE [id] in (" + ids + ")",
                                                DbFields.USERGROUPINFO,
                                                BaseConfigs.GetTablePrefix);
            return DbHelper.ExecuteNonQuery(CommandType.Text, commandText, parms);
            
        }

        public int DelUser(string ids)
        {
            ids = LTJ.Common.Utils.returnIdList(ids);
            DbParameter[] parms = {
									   DbHelper.MakeInParam("@ids",(DbType)SqlDbType.VarChar,2000,ids)
			                      };
            string commandText = string.Format("Delete  [{1}user] WHERE [id] in (" + ids + ")",
                                                DbFields.USERINFO,
                                                BaseConfigs.GetTablePrefix);
            return DbHelper.ExecuteNonQuery(CommandType.Text, commandText, parms);            
        }

        /// <summary>
        /// 更新用户状态信息
        /// </summary>
        public bool UpdateUserStatus(string ids, int status)
        {
            string commandText = string.Format("UPDATE [{0}user] SET [status] = {1} WHERE [id] IN ({2})", BaseConfigs.GetTablePrefix, status, ids);
            return DbHelper.ExecuteNonQuery(CommandType.Text, commandText) > 0;
        }

        /// <summary>
        /// 更新用户所属管理员ID
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="fid">管理员ID</param>
        public bool UpdateUserFid(int uid, int fid)
        {
            string commandText = string.Format("UPDATE [{0}user] SET [fid] = {1} WHERE [id] = {2}", BaseConfigs.GetTablePrefix, fid, uid);
            return DbHelper.ExecuteNonQuery(CommandType.Text, commandText) > 0;
        }
        /// <summary>
        /// 批量更新用户所属管理员ID
        /// </summary>
        public bool UpdateUsersFid(int fid, string ids)
        {
            string commandText = string.Format("UPDATE [{0}user] SET [fid] = {1} WHERE [id] IN ({2})", BaseConfigs.GetTablePrefix, fid, ids);
            return DbHelper.ExecuteNonQuery(CommandType.Text, commandText) > 0;
        }
        /// <summary>
        /// 检查密码
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="originalpassword">是否非MD5密码</param>
        /// <returns>如果正确则返回用户id, 否则返回-1</returns>
        public IDataReader CheckPassword(string userName, string passWord, bool originalPassWord)
        {
            DbParameter[] parms = {
									   DbHelper.MakeInParam("@username",(DbType)SqlDbType.VarChar,50, userName),
									   DbHelper.MakeInParam("@password",(DbType)SqlDbType.VarChar,200, originalPassWord ? Utils.MD5(passWord) : passWord)
								   };
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, string.Format("{0}checkpasswordbyusername", BaseConfigs.GetTablePrefix), parms);
        }

        /// <summary>
        /// 检测密码
        /// </summary>
        /// <param name="uid">用户id</param>
        /// <param name="password">密码</param>
        /// <param name="originalpassword">是否非MD5密码</param>
        /// <param name="groupid">用户组id</param>
        /// <param name="adminid">管理id</param>
        /// <returns>如果用户密码正确则返回uid, 否则返回-1</returns>
        public IDataReader CheckPassword(int uid, string passWord, bool originalPassWord)
        {
            DbParameter[] parms = {
									   DbHelper.MakeInParam("@uid",(DbType)SqlDbType.Int,4,uid),
									   DbHelper.MakeInParam("@password",(DbType)SqlDbType.VarChar,200, originalPassWord ? Utils.MD5(passWord) : passWord)
								   };
            string pp = Utils.MD5(passWord);
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, string.Format("{0}checkpasswordbyuid", BaseConfigs.GetTablePrefix), parms);
        }

        /// <summary>
        /// 创建新用户.
        /// </summary>
        /// <param name="__userinfo">用户信息</param>
        /// <returns>返回用户ID, 如果已存在该用户名则返回-1</returns>
        public int CreateUserGroup(UserGroupInfo usergroupInfo)
        {
            DbParameter[] parms = {
					DbHelper.MakeInParam("@name", (DbType)SqlDbType.NVarChar,20,usergroupInfo.name),
					DbHelper.MakeInParam("@sort", (DbType)SqlDbType.Int,4,usergroupInfo.sort),
					DbHelper.MakeInParam("@auths", (DbType)SqlDbType.Int,4,usergroupInfo.auths),
					DbHelper.MakeInParam("@status", (DbType)SqlDbType.Int,4,usergroupInfo.status),
					DbHelper.MakeInParam("@backup", (DbType)SqlDbType.VarChar,8000,usergroupInfo.backup)
                                  };

            return TypeConverter.ObjectToInt(DbHelper.ExecuteScalar(CommandType.StoredProcedure, string.Format("{0}usergroup_ADD", BaseConfigs.GetTablePrefix), parms), -1);
        }

        /// <summary>
        /// 根据用户组名获取用户组信息
        /// </summary>
        /// <param name="usergroupName">用户组信息</param>
        /// <returns></returns>
        public IDataReader GetUserGroupInfoByName(string usergroupName)
        {
            DbParameter[] parms = {
									   DbHelper.MakeInParam("@usergroupname",(DbType)SqlDbType.VarChar,20,usergroupName),
			                      };
            string commandText = string.Format("SELECT TOP 1 {0} FROM [{1}usergroup] WHERE [name]=@usergroupname",
                                                DbFields.USERGROUPINFO,
                                                BaseConfigs.GetTablePrefix);
            return DbHelper.ExecuteReader(CommandType.Text, commandText, parms);
        }

        /// <summary>
        /// 根据where条件 取得用户组数据列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataTable GetUserGroupList(string strWhere)
        {

            DbParameter[] parms = {
                                       DbHelper.MakeInParam("@table",(DbType)SqlDbType.VarChar,4000,string.Format("{0}usergroup", BaseConfigs.GetTablePrefix)),
                                       DbHelper.MakeInParam("@select",(DbType)SqlDbType.VarChar,4000, DbFields.USERGROUPINFO),
									   DbHelper.MakeInParam("@strwhere",(DbType)SqlDbType.VarChar,2000,strWhere),
			                      };
            return DbHelper.ExecuteDataset(CommandType.StoredProcedure, string.Format("{0}GetDataList", BaseConfigs.GetTablePrefix), parms).Tables[0];
        }

        /// <summary>
        /// 删除用户组信息
        /// </summary>
        /// <param name="ids">用户组id组合</param>
        /// <returns></returns>
        public int DelUserGroup(string ids)
        {
            ids = LTJ.Common.Utils.returnIdList(ids);
            DbParameter[] parms = {
									   DbHelper.MakeInParam("@ids",(DbType)SqlDbType.VarChar,2000,ids)
			                      };
            string commandText = string.Format("Delete  [{1}usergroup] WHERE [id] in ("+ids+")",
                                                DbFields.USERGROUPINFO,
                                                BaseConfigs.GetTablePrefix);
            return DbHelper.ExecuteNonQuery(CommandType.Text, commandText, parms);
        }

        /// <summary>
        /// 根据用户组id获取用户组实体
        /// </summary>
        /// <param name="gid">用户组id</param>
        /// <returns></returns>
        public IDataReader GetUserGroupInfoByID(int gid)
        {
            DbParameter[] parms = {
                                      DbHelper.MakeInParam("@id", (DbType)SqlDbType.Int,4, gid)
                                  };
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, string.Format("{0}usergroup_GetModel", BaseConfigs.GetTablePrefix), parms);
        }
        /// <summary>
        /// 编辑用户组
        /// </summary>
        public bool UpdateUserGroup(LTJ.Entity.UserGroupInfo ugInfo)
        {
            DbParameter[] parms = {
                                      DbHelper.MakeInParam("@id", (DbType)SqlDbType.Int,4,ugInfo.id),
					DbHelper.MakeInParam("@name", (DbType)SqlDbType.NVarChar,20,ugInfo.name),
					DbHelper.MakeInParam("@sort", (DbType)SqlDbType.Int,4,ugInfo.sort),
					DbHelper.MakeInParam("@auths", (DbType)SqlDbType.Int,4,ugInfo.auths),
					DbHelper.MakeInParam("@status", (DbType)SqlDbType.Int,4,ugInfo.status),
					DbHelper.MakeInParam("@backup", (DbType)SqlDbType.VarChar,8000,ugInfo.backup)
		    };
            return DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, string.Format("{0}usergroup_Update", BaseConfigs.GetTablePrefix), parms) > 0;
        }

        /// <summary>
        /// 设置企业管理员权限
        /// </summary>
        /// <param name="cid">企业ID</param>
        /// <param name="uid">用户ID</param>
        /// <param name="iscreater">是否超级管理员</param>
        /// <param name="authorities">权限集合</param>
        /// <returns>更新是否成功</returns>
        public bool SetCorpManagerAuth(int cid, int uid, bool iscreater, string authorities)
        {
            DbParameter[] parms = {
                    DbHelper.MakeInParam("@cid", (DbType)SqlDbType.Int,4,cid),
					DbHelper.MakeInParam("@uid", (DbType)SqlDbType.Int,4,uid),
                    DbHelper.MakeInParam("@iscreater", (DbType)SqlDbType.Bit,1,iscreater),
					DbHelper.MakeInParam("@auth", (DbType)SqlDbType.VarChar,0,authorities)
		    };
            return DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, string.Format("{0}corpmanager_SET", BaseConfigs.GetTablePrefix), parms) > 0;
        }

        /// <summary>
        /// 获取权限实体
        /// </summary>
        /// <param name="cid">企业ID</param>
        /// <param name="uid">用户ID</param>
        /// <returns>权限实体</returns>
        public IDataReader GetAuthInfo(int cid, int uid)
        {
            DbParameter[] parms = {
                                      DbHelper.MakeInParam("@cid", (DbType)SqlDbType.Int,4, cid),
                                      DbHelper.MakeInParam("@uid", (DbType)SqlDbType.Int,4, uid)
                                  };
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, string.Format("{0}authority_GetModel", BaseConfigs.GetTablePrefix), parms);
        }

        /// <summary>
        /// 获取企业管理员权限
        /// </summary>
        public DataTable GetCorpManagerAuthList(int pageindex, int pagesize, string condition)
        {
            DbParameter[] parms = {
                          DbHelper.MakeInParam("@pageindex",(DbType)SqlDbType.Int,4,pageindex),
                          DbHelper.MakeInParam("@pagesize",(DbType)SqlDbType.Int,4,pagesize),
                          DbHelper.MakeInParam("@conditions",(DbType)SqlDbType.NVarChar,2000,condition)
            };
            return DbHelper.ExecuteDataset(CommandType.StoredProcedure, string.Format("{0}getauthoritylistbypage", BaseConfigs.GetTablePrefix), parms).Tables[0];
        }

        /// <summary>
        /// 根据条件企业管理员数量
        /// </summary>
        public int GetCorpManagerAuthCount(string condition)
        {
            DbParameter[] parms = { DbHelper.MakeInParam("@conditions", (DbType)SqlDbType.VarChar, 2000, condition) };
            return TypeConverter.ObjectToInt(DbHelper.ExecuteScalar(CommandType.StoredProcedure, string.Format("{0}getauthcountbycondition", BaseConfigs.GetTablePrefix), parms));
        }

        /// <summary>
        /// 批量删除企业管理员
        /// </summary>
        public void DeleteCorpManagerAuth(string idsparm)
        {
            string strWhere = "";

            foreach (string str in idsparm.Split(','))
            {
                string[] substr = str.Split('_');

                if (substr.Length == 2)
                {
                    strWhere += " (cid = " + substr[0] + " AND uid = " + substr[1] + ") OR ";
                }
            }

            if (strWhere.Length > 3) strWhere = strWhere.Substring(0, strWhere.Length - 3);

            string commandText = string.Format("Delete [{0}authority] {1}", BaseConfigs.GetTablePrefix, strWhere.Trim() == "" ? "" : " WHERE " + strWhere);
            DbHelper.ExecuteNonQuery(CommandType.Text, commandText);
        }

        /// <summary>
        /// 根据用户ID获取用户权限列表
        /// </summary>
        public IDataReader GetAuthListByUid(int uid)
        {
            DbParameter[] parms = {
                                      DbHelper.MakeInParam("@uid", (DbType)SqlDbType.Int,4, uid)
                                  };
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, string.Format("{0}getauthoritybyuid", BaseConfigs.GetTablePrefix), parms);
        }

        #region oluser 在线用户操作
        public IDataReader GetOnlineUserByID(int olid)
        {
            DbParameter[] parms = {
                                      DbHelper.MakeInParam("@id", (DbType)SqlDbType.Int,4, olid)
                                  };
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, string.Format("{0}oluser_GetModel", BaseConfigs.GetTablePrefix), parms);
        }
        /// <summary>
        /// 获得指定用户的详细信息
        /// </summary>
        /// <param name="userId">在线用户ID</param>
        /// <param name="password">用户密码</param>
        /// <returns>用户的详细信息</returns>
        public DataTable GetOnlineUser(int userId, string passWord)
        {
            DbParameter[] parms =  { 
                                        DbHelper.MakeInParam("@userid", (DbType)SqlDbType.Int, 4, userId),
                                        DbHelper.MakeInParam("@password", (DbType)SqlDbType.Char, 32, passWord)
                                    };
            return DbHelper.ExecuteDataset(CommandType.StoredProcedure, string.Format("{0}getonlineuser", BaseConfigs.GetTablePrefix), parms).Tables[0];
        }
        /// <summary>
        /// 执行在线用户向表及缓存中添加的操作。
        /// </summary>
        /// <param name="onlineuserinfo">在组用户信息内容</param>
        /// <param name="timeout">系统设置用户多少时间即算做离线</param>
        /// <param name="deletingfrequency">删除过期用户频率(单位:分钟)</param>
        /// <returns>添加成功则返回刚刚添加的olid,失败则返回0</returns>
        public int AddOnlineUser(OnlineUserInfo onlineUserInfo, int timeOut, int deletingFrequency)
        {
            //标识需要更新用户在线状态，0表示需要更新
            int onlinestate = 1;

            // 如果timeout为负数则代表不需要精确更新用户是否在线的状态
            if (timeOut > 0)
            {
                if (onlineUserInfo.uid > 0)
                    onlinestate = 0;
            }
            else
                timeOut = timeOut * -1;

            if (timeOut > 9999)
                timeOut = 9999;

            DbParameter[] parms = {
									   DbHelper.MakeInParam("@onlinestate",(DbType)SqlDbType.Int,4,onlinestate),
					                   DbHelper.MakeInParam("@uid", (DbType)SqlDbType.Int,4,onlineUserInfo.uid),
				                       DbHelper.MakeInParam("@olip", (DbType)SqlDbType.VarChar,20,onlineUserInfo.olip),
				                       DbHelper.MakeInParam("@lname", (DbType)SqlDbType.VarChar,20,onlineUserInfo.lname),
				                       DbHelper.MakeInParam("@rname", (DbType)SqlDbType.NVarChar,20,onlineUserInfo.rname),
				                       DbHelper.MakeInParam("@password", (DbType)SqlDbType.VarChar,50,onlineUserInfo.password),
				                       DbHelper.MakeInParam("@groupid", (DbType)SqlDbType.Int,4,onlineUserInfo.groupid),
				                       DbHelper.MakeInParam("@uimg", (DbType)SqlDbType.VarChar,200,onlineUserInfo.uimg),
				                       DbHelper.MakeInParam("@lastupdate", (DbType)SqlDbType.DateTime,8,DateTime.Parse(onlineUserInfo.lastupdate)),
				                       DbHelper.MakeInParam("@newpms", (DbType)SqlDbType.Int,4,onlineUserInfo.newpms),
				                       DbHelper.MakeInParam("@verifycode", (DbType)SqlDbType.Char,10,onlineUserInfo.verifycode)
								   };
            int olid = TypeConverter.ObjectToInt(DbHelper.ExecuteScalar(CommandType.StoredProcedure,
                                                                        string.Format("{0}oluser_ADD", BaseConfigs.GetTablePrefix),
                                                                        parms));

            //按照系统设置频率(默认5分钟)清除过期用户
            if (_lastRemoveTimeout == 0 || (System.Environment.TickCount - _lastRemoveTimeout) > 60000 * deletingFrequency)
            {
                DeleteExpiredOnlineUsers(timeOut);
                _lastRemoveTimeout = System.Environment.TickCount;
            }
            // 如果id值太大则重建在线表
            if (olid > 2147483000)
            {
                CreateOnlineTable();
                DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, string.Format("{0}oluser_ADD", BaseConfigs.GetTablePrefix), parms);
                return 1;
            }
            return 0;
        }

        private void DeleteExpiredOnlineUsers(int timeOut)
        {
            System.Text.StringBuilder timeoutStrBuilder = new System.Text.StringBuilder();
            System.Text.StringBuilder memberStrBuilder = new System.Text.StringBuilder();

            DbParameter param = DbHelper.MakeInParam("@expires", (DbType)SqlDbType.DateTime, 8, DateTime.Now.AddMinutes(timeOut * -1));
            IDataReader dr = DbHelper.ExecuteReader(CommandType.StoredProcedure,
                                                    string.Format("{0}getexpiredonlineuserlist", BaseConfigs.GetTablePrefix),
                                                    param);
            while (dr.Read())
            {
                timeoutStrBuilder.Append(",");
                timeoutStrBuilder.Append(dr["id"].ToString());
                if (dr["uid"].ToString() != "-1")
                {
                    memberStrBuilder.Append(",");
                    memberStrBuilder.Append(dr["uid"].ToString());
                }
            }
            dr.Close();

            if (timeoutStrBuilder.Length > 0)
            {
                timeoutStrBuilder.Remove(0, 1);
                DbHelper.ExecuteNonQuery(CommandType.StoredProcedure,
                                         string.Format("{0}deleteonlineusers", BaseConfigs.GetTablePrefix),
                                         DbHelper.MakeInParam("@olidlist", (DbType)SqlDbType.VarChar, 5000, timeoutStrBuilder.Length <= 5000 ? timeoutStrBuilder.ToString() : timeoutStrBuilder.ToString().Substring(0, 5000).TrimEnd(',')));
            }
            if (memberStrBuilder.Length > 0)
            {
                memberStrBuilder.Remove(0, 1);
                DbHelper.ExecuteNonQuery(CommandType.StoredProcedure,
                                         string.Format("{0}updateuseronlinestates", BaseConfigs.GetTablePrefix),
                                         DbHelper.MakeInParam("@uidlist", (DbType)SqlDbType.VarChar, 5000, memberStrBuilder.Length <= 5000 ? memberStrBuilder.ToString() : memberStrBuilder.ToString().Substring(0, 5000).TrimEnd(',')));
            }
        }

        /// <summary>
        /// 创建在线表
        /// </summary>
        /// <returns></returns>
        public int CreateOnlineTable()
        {
            try
            {
                return DbHelper.ExecuteNonQuery(CommandType.Text, string.Format("TRUNCATE TABLE [{0}oluser]", BaseConfigs.GetTablePrefix));
            }
            catch
            {
                return -1;
            }
        }

        /// <summary>
        /// 设置用户在线状态
        /// </summary>
        /// <param name="uid">用户Id</param>
        /// <param name="onlinestate">在线状态，1在线</param>
        /// <returns></returns>
        public int SetUserOnlineState(int uid, int onlineState, string vcode)
        {
            string commandText = string.Format("UPDATE [{0}user] SET [o_status]={1},[lastdate]=GETDATE(),[lcount]=[lcount]+1,[olcode] = '{3}' WHERE [id]={2}",
                                                BaseConfigs.GetTablePrefix,
                                                onlineState,
                                                uid, vcode);
            return DbHelper.ExecuteNonQuery(CommandType.Text, commandText);
        }
        /// <summary>
        /// 更新用户IP地址
        /// </summary>
        /// <param name="olid">在线id</param>
        /// <param name="ip">ip地址</param>
        public void UpdateIP(int olId, string ip)
        {
            DbParameter[] parms = {
									   DbHelper.MakeInParam("@ip",(DbType)SqlDbType.VarChar,20,ip),
									   DbHelper.MakeInParam("@olid",(DbType)SqlDbType.Int,4,olId)
								   };
            string commandText = string.Format("UPDATE [{0}oluser] SET [olip]=@ip WHERE [id]=@olid", BaseConfigs.GetTablePrefix);
            DbHelper.ExecuteNonQuery(CommandType.Text, commandText, parms);
        }
        /// <summary>
        /// 删除符合条件的一个或多个用户信息
        /// </summary>
        /// <returns>删除行数</returns>
        public int DeleteRowsByIP(string ip)
        {
            DbParameter[] parms = {
									   DbHelper.MakeInParam("@ip",(DbType)SqlDbType.VarChar,20,ip)
								   };
            string commandText = string.Format("UPDATE [{0}user] SET [o_status]=0,[lastdate]=GETDATE() WHERE [id] IN (SELECT [uid] FROM [{0}oluser] WHERE [uid]>0 AND [olip]=@ip)",
                                                BaseConfigs.GetTablePrefix);
            DbHelper.ExecuteNonQuery(CommandType.Text, commandText, parms);
            if (ip != "0.0.0.0")
                return DbHelper.ExecuteNonQuery(CommandType.Text, string.Format("DELETE FROM [{0}oluser] WHERE [uid]=-1 AND [olip]=@ip", BaseConfigs.GetTablePrefix), parms);
            return 0;
        }

        /// <summary>
        /// 删除在线表中指定在线id的行
        /// </summary>
        /// <param name="olid">在线id</param>
        /// <returns></returns>
        public int DeleteRows(int olId)
        {
            string commandText = string.Format("DELETE FROM [{0}oluser] WHERE [id]={1}", BaseConfigs.GetTablePrefix, olId);
            return DbHelper.ExecuteNonQuery(CommandType.Text, commandText);
        }
        /// <summary>
        /// 获得指定用户的详细信息
        /// </summary>
        /// <param name="userId">在线用户ID</param>
        /// <param name="ip">IP</param>
        /// <returns></returns>
        public DataTable GetOnlineUserByIP(int userId, string ip)
        {
            DbParameter[] parms = { 
                                        DbHelper.MakeInParam("@userid", (DbType)SqlDbType.Int, 4, userId),
                                        DbHelper.MakeInParam("@ip", (DbType)SqlDbType.VarChar, 15, ip)
                                    };
            return DbHelper.ExecuteDataset(CommandType.StoredProcedure, string.Format("{0}getonlineuserbyip", BaseConfigs.GetTablePrefix), parms).Tables[0];
        }
        /// <summary>
        /// 检查在线用户验证码是否有效
        /// </summary>
        /// <param name="olid">在组用户ID</param>
        /// <param name="verifycode">验证码</param>
        /// <returns>在组用户ID</returns>
        public bool CheckUserVerifyCode(int olId, string verifyCode, string newVerifyCode)
        {
            DbParameter[] parms = { 
                                        DbHelper.MakeInParam("@olid", (DbType)SqlDbType.Int, 4, olId),
                                        DbHelper.MakeInParam("@verifycode", (DbType)SqlDbType.VarChar, 10, verifyCode)
                                    };
            string commandText = string.Format("SELECT TOP 1 [id] FROM [{0}oluser] WHERE [id]=@olid and [verifycode]=@verifycode",
                                                BaseConfigs.GetTablePrefix);
            DataTable dt = DbHelper.ExecuteDataset(CommandType.Text, commandText, parms).Tables[0];
            parms[1].Value = newVerifyCode;

            commandText = string.Format("UPDATE [{0}oluser] SET [verifycode]=@verifycode WHERE [id]=@olid", BaseConfigs.GetTablePrefix);
            DbHelper.ExecuteNonQuery(CommandType.Text, commandText, parms);
            return dt.Rows.Count > 0;
        }
        /// <summary>
        /// 更新在线时间
        /// </summary>
        /// <param name="olTimeSpan">在线时间间隔</param>
        /// <param name="uid">当前用户id</param>
        public void UpdateOnlineTime(int oltimeSpan, int uid)
        {
            DbParameter[] parms = {
                                    DbHelper.MakeInParam("@uid", (DbType)SqlDbType.Int, 4, uid),
                                    DbHelper.MakeInParam("@oltimespan", (DbType)SqlDbType.SmallInt, 2, oltimeSpan),
                                    DbHelper.MakeInParam("@lastupdate", (DbType)SqlDbType.DateTime, 8, DateTime.Now),
                                    DbHelper.MakeInParam("@expectedlastupdate", (DbType)SqlDbType.DateTime, 8, DateTime.Now.AddMinutes(0 - oltimeSpan))
                                };
            string commandText = string.Format("UPDATE [{0}onlinetime] SET [thismonth]=[thismonth]+@oltimespan, [total]=[total]+@oltimespan, [lastupdate]=@lastupdate WHERE [uid]=@uid AND [lastupdate]<=@expectedlastupdate",
                                                BaseConfigs.GetTablePrefix);
            if (DbHelper.ExecuteNonQuery(CommandType.Text, commandText, parms) < 1)
            {
                try
                {
                    commandText = string.Format("INSERT INTO [{0}onlinetime]([uid], [thismonth], [total], [lastupdate]) VALUES(@uid, @oltimespan, @oltimespan, @lastupdate)",
                                                 BaseConfigs.GetTablePrefix);
                    DbHelper.ExecuteNonQuery(CommandType.Text, commandText, parms);
                }
                catch { }
            }
        }

        /// <summary>
        /// 同步在线时间
        /// </summary>
        /// <param name="uid">用户id</param>
        public void SynchronizeOnlineTime(int uid)
        {
            DbParameter[] parms = {
                                    DbHelper.MakeInParam("@uid", (DbType)SqlDbType.Int, 4, uid),
                                  };
            string commandText = string.Format("SELECT [total] FROM [{0}onlinetime] WHERE [uid]=@uid", BaseConfigs.GetTablePrefix);
            int total = TypeConverter.ObjectToInt(DbHelper.ExecuteScalar(CommandType.Text, commandText, parms));

            commandText = string.Format("UPDATE [{0}user] SET [oltime]={1} WHERE [oltime]<{1} AND [id]=@uid", BaseConfigs.GetTablePrefix, total);
            if (DbHelper.ExecuteNonQuery(CommandType.Text, commandText, parms) < 1)
            {
                try
                {
                    commandText = string.Format("UPDATE [{0}onlinetime] SET [total]=(SELECT [oltime] FROM [{0}user] WHERE [id]=@uid) WHERE [uid]=@uid",
                                                 BaseConfigs.GetTablePrefix);
                    DbHelper.ExecuteNonQuery(CommandType.Text, commandText, parms);
                }
                catch { }
            }
        }

        /// <summary>
        /// 更新在线操作
        /// </summary>
        public void UpdateAction(int olid, int actid)
        {
            DbParameter[] parms = {
									DbHelper.MakeInParam("@action",(DbType)SqlDbType.SmallInt,2,actid),
                                    DbHelper.MakeInParam("@lastupdate", (DbType)SqlDbType.DateTime, 8, DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))),
									DbHelper.MakeInParam("@olid",(DbType)SqlDbType.Int,4,olid)
								  };
            DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, string.Format("{0}updateonlineaction", BaseConfigs.GetTablePrefix), parms);
        }
        /// <summary>
        /// 删除指定在线用户信息
        /// </summary>
        /// <param name="uid"></param>
        public void DeleteRowsByUid(int uid)
        {
            string commandText = string.Format("DELETE FROM [{0}oluser] WHERE [uid]={1}", BaseConfigs.GetTablePrefix, uid);
            DbHelper.ExecuteNonQuery(CommandType.Text, commandText);
        }
        #endregion

        #region 日志操作
        /// <summary>
        /// 创建访问日志
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="lname">登录名</param>
        /// <param name="gid">组ID</param>
        /// <param name="cid">访问企业ID</param>
        /// <param name="ip">访问IP</param>
        /// <param name="actid">访问动作</param>
        /// <param name="actdesc">具体内容描述</param>
        /// <returns>日志ID</returns>
        public int CreateVisitLog(int uid, string lname, int gid, int cid, string ip, int actid, string actstr, string actdesc)
        {
            DbParameter[] parms = {
					                DbHelper.MakeInParam("@uid", (DbType)SqlDbType.Int,4,uid),
					                DbHelper.MakeInParam("@lname", (DbType)SqlDbType.VarChar,20,lname),
					                DbHelper.MakeInParam("@gid", (DbType)SqlDbType.Int,4,gid),
					                DbHelper.MakeInParam("@visitip", (DbType)SqlDbType.VarChar,15,ip),
					                DbHelper.MakeInParam("@postdatetime", (DbType)SqlDbType.SmallDateTime,8, DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))),
					                DbHelper.MakeInParam("@actions", (DbType)SqlDbType.NVarChar,100,actstr),
					                DbHelper.MakeInParam("@others", (DbType)SqlDbType.NVarChar,200,actdesc),
					                DbHelper.MakeInParam("@cid", (DbType)SqlDbType.Int,4,cid),
					                DbHelper.MakeInParam("@actid", (DbType)SqlDbType.Int,4,actid) 
                                  };
            return TypeConverter.ObjectToInt(DbHelper.ExecuteScalar(CommandType.StoredProcedure, string.Format("{0}adminvisitlog_ADD", BaseConfigs.GetTablePrefix), parms), -1);
        }
        /// <summary>
        /// 根据条件获取全部企业管理员访问日志集合数量
        /// </summary>
        public int GetAllCompanyVisitLogCount(string querystring)
        {
            DbParameter[] parms = { DbHelper.MakeInParam("@conditions", (DbType)SqlDbType.VarChar, 2000, querystring) };
            return TypeConverter.ObjectToInt(DbHelper.ExecuteScalar(CommandType.StoredProcedure, string.Format("{0}getadminvisitelogcountbycondition", BaseConfigs.GetTablePrefix), parms));
        }
        /// <summary>
        /// 获取全部企业管理员访问日志分页操作
        /// </summary>
        public DataTable GetAllCompanyVisitLogsByPage(int pageindex, int pagesize, string condition, string ordercolumn, string ordertype)
        {
            DbParameter[] parms = {
                          DbHelper.MakeInParam("@pageindex",(DbType)SqlDbType.Int,4,pageindex),
                          DbHelper.MakeInParam("@pagesize",(DbType)SqlDbType.Int,4,pagesize),
                          DbHelper.MakeInParam("@conditions",(DbType)SqlDbType.NVarChar,2000,condition),
                          DbHelper.MakeInParam("@ordercolumn",(DbType)SqlDbType.VarChar,20,ordercolumn),
                          DbHelper.MakeInParam("@ordertype",(DbType)SqlDbType.VarChar,5,ordertype)
            };
            return DbHelper.ExecuteDataset(CommandType.StoredProcedure, string.Format("{0}getadminvisitelogbypage", BaseConfigs.GetTablePrefix), parms).Tables[0];
        }
        /// <summary>
        /// 删除访问日志
        /// </summary>
        public void DelVisitLog(string ids)
        {
            string commandText = string.Format("DELETE [{0}adminvisitlog] WHERE [id] IN ({1})", BaseConfigs.GetTablePrefix, ids);
            DbHelper.ExecuteNonQuery(CommandType.Text, commandText);
        }
        /// <summary>
        /// 创建线路信息日志
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="lname">登录名</param>
        /// <param name="rid">新增线路ID</param>
        /// <param name="price1">20GP</param>
        /// <param name="price2">40GP</param>
        /// <param name="price3">40HQ</param>
        /// <param name="actid">动作ID</param>
        /// <param name="logstr">动作描述</param>
        public int CreateRouteLog(int uid, string lname, int rid, string price1, string price2, string price3, int actid, string actstr, string actdesc)
        {
            DbParameter[] parms = {
					                DbHelper.MakeInParam("@uid", (DbType)SqlDbType.Int,4,uid),
					                DbHelper.MakeInParam("@lname", (DbType)SqlDbType.Char,10,lname),
					                DbHelper.MakeInParam("@rid", (DbType)SqlDbType.Int,4,rid),
					                DbHelper.MakeInParam("@createdate", (DbType)SqlDbType.SmallDateTime,8, DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))),
					                DbHelper.MakeInParam("@20GP", (DbType)SqlDbType.VarChar,50,price1),
					                DbHelper.MakeInParam("@40GP", (DbType)SqlDbType.VarChar,50,price2),
					                DbHelper.MakeInParam("@40HQ", (DbType)SqlDbType.VarChar,50,price3),
					                DbHelper.MakeInParam("@actions", (DbType)SqlDbType.NVarChar,100,actstr),
					                DbHelper.MakeInParam("@others", (DbType)SqlDbType.NVarChar,200,actdesc),
					                DbHelper.MakeInParam("@actid", (DbType)SqlDbType.Int,4,actid)
                                  };
            return TypeConverter.ObjectToInt(DbHelper.ExecuteScalar(CommandType.StoredProcedure, string.Format("{0}routelog_ADD", BaseConfigs.GetTablePrefix), parms), -1);
        }
        /// <summary>
        /// 根据条件获取全部企业线路操作日志集合数量
        /// </summary>
        public int GetAllRouteLogCount(string querystring)
        {
            DbParameter[] parms = { DbHelper.MakeInParam("@conditions", (DbType)SqlDbType.VarChar, 2000, querystring) };
            return TypeConverter.ObjectToInt(DbHelper.ExecuteScalar(CommandType.StoredProcedure, string.Format("{0}getroutelogcountbycondition", BaseConfigs.GetTablePrefix), parms));
        }
        /// <summary>
        /// 获取全部企业线路操作日志分页操作
        /// </summary>
        public DataTable GetRouteLogsByPage(int pageindex, int pagesize, string condition, string ordercolumn, string ordertype)
        {
            DbParameter[] parms = {
                          DbHelper.MakeInParam("@pageindex",(DbType)SqlDbType.Int,4,pageindex),
                          DbHelper.MakeInParam("@pagesize",(DbType)SqlDbType.Int,4,pagesize),
                          DbHelper.MakeInParam("@conditions",(DbType)SqlDbType.NVarChar,2000,condition),
                          DbHelper.MakeInParam("@ordercolumn",(DbType)SqlDbType.VarChar,20,ordercolumn),
                          DbHelper.MakeInParam("@ordertype",(DbType)SqlDbType.VarChar,5,ordertype)
            };
            return DbHelper.ExecuteDataset(CommandType.StoredProcedure, string.Format("{0}getroutelogbypage", BaseConfigs.GetTablePrefix), parms).Tables[0];
        }
        /// <summary>
        /// 删除线路操作日志
        /// </summary>
        public void DelRouteLogs(string ids)
        {
            string commandText = string.Format("DELETE [{0}routelog] WHERE [id] IN ({1})", BaseConfigs.GetTablePrefix, ids);
            DbHelper.ExecuteNonQuery(CommandType.Text, commandText);
        }
        /// <summary>
        /// 根据条件获取线路日志集合
        /// </summary>
        public DataTable GetRouteLogTable(string conditions)
        {
            DbParameter[] parms = { DbHelper.MakeInParam("@conditions", (DbType)SqlDbType.VarChar, 2000, conditions) };
            return DbHelper.ExecuteDataset(CommandType.StoredProcedure, string.Format("{0}getrouteloglistbycondition", BaseConfigs.GetTablePrefix), parms).Tables[0];
        }
        /// <summary>
        /// 根据条件获取线路日志集合
        /// </summary>
        public IDataReader GetRouteLogReader(string conditions)
        {
            DbParameter[] parms = { DbHelper.MakeInParam("@conditions", (DbType)SqlDbType.VarChar, 2000, conditions) };
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, string.Format("{0}getrouteloglistbycondition", BaseConfigs.GetTablePrefix), parms);
        }
        /// <summary>
        /// 创建规则日志
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="lname">登录名</param>
        /// <param name="cid">规则所属公司</param>
        /// <param name="rid">规则ID</param>
        /// <param name="price1">20GP</param>
        /// <param name="price2">40GP</param>
        /// <param name="price3">40HQ</param>
        /// <param name="actid">动作ID</param>
        /// <param name="actdesc">动作描述</param>
        public int CreateRuleLog(int uid, string lname, int cid, int rid, string price1, string price2, string price3, int actid, string actstr, string actdesc)
        {
            DbParameter[] parms = {
					                DbHelper.MakeInParam("@uid", (DbType)SqlDbType.Int,4,uid),
					                DbHelper.MakeInParam("@lname", (DbType)SqlDbType.Char,10,lname),
					                DbHelper.MakeInParam("@rid", (DbType)SqlDbType.Int,4,rid),
                                    DbHelper.MakeInParam("@cid",(DbType)SqlDbType.Int,4,cid),
					                DbHelper.MakeInParam("@createdate", (DbType)SqlDbType.SmallDateTime,8, DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))),
					                DbHelper.MakeInParam("@20GP", (DbType)SqlDbType.VarChar,50,price1),
					                DbHelper.MakeInParam("@40GP", (DbType)SqlDbType.VarChar,50,price2),
					                DbHelper.MakeInParam("@40HQ", (DbType)SqlDbType.VarChar,50,price3),
					                DbHelper.MakeInParam("@actions", (DbType)SqlDbType.NVarChar,100,actstr),
					                DbHelper.MakeInParam("@others", (DbType)SqlDbType.NVarChar,200,actdesc),
					                DbHelper.MakeInParam("@actid", (DbType)SqlDbType.Int,4,actid)
                                  };
            return TypeConverter.ObjectToInt(DbHelper.ExecuteScalar(CommandType.StoredProcedure, string.Format("{0}rulelog_ADD", BaseConfigs.GetTablePrefix), parms), -1);
        }
        /// <summary>
        /// 根据条件获取全部企业规则操作日志集合数量
        /// </summary>
        public int GetAllRuleLogCount(string querystring)
        {
            DbParameter[] parms = { DbHelper.MakeInParam("@conditions", (DbType)SqlDbType.VarChar, 2000, querystring) };
            return TypeConverter.ObjectToInt(DbHelper.ExecuteScalar(CommandType.StoredProcedure, string.Format("{0}getrulelogcountbycondition", BaseConfigs.GetTablePrefix), parms));
        }
        /// <summary>
        /// 获取全部企业规则操作日志分页操作
        /// </summary>
        public DataTable GetRuleLogsByPage(int pageindex, int pagesize, string condition, string ordercolumn, string ordertype)
        {
            DbParameter[] parms = {
                          DbHelper.MakeInParam("@pageindex",(DbType)SqlDbType.Int,4,pageindex),
                          DbHelper.MakeInParam("@pagesize",(DbType)SqlDbType.Int,4,pagesize),
                          DbHelper.MakeInParam("@conditions",(DbType)SqlDbType.NVarChar,2000,condition),
                          DbHelper.MakeInParam("@ordercolumn",(DbType)SqlDbType.VarChar,20,ordercolumn),
                          DbHelper.MakeInParam("@ordertype",(DbType)SqlDbType.VarChar,5,ordertype)
            };
            return DbHelper.ExecuteDataset(CommandType.StoredProcedure, string.Format("{0}getrulelogbypage", BaseConfigs.GetTablePrefix), parms).Tables[0];
        }
        /// <summary>
        /// 删除规则操作日志
        /// </summary>
        public void DelRuleLogs(string ids)
        {
            string commandText = string.Format("DELETE [{0}rulelog] WHERE [id] IN ({1})", BaseConfigs.GetTablePrefix, ids);
            DbHelper.ExecuteNonQuery(CommandType.Text, commandText);
        }
        /// <summary>
        /// 创建用户搜索日志
        /// </summary>
        /// <param name="uid">用户ID（-1为匿名用户）</param>
        /// <param name="lname">登录名</param>
        /// <param name="visitip">访问IP</param>
        /// <param name="stype">搜索类型（默认0为整柜）</param>
        /// <param name="dportkey">目的港关键字</param>
        /// <param name="lportkey">起运港关键字</param>
        /// <param name="routeline">航线</param>
        /// <param name="boatcorp">船公司</param>
        /// <param name="oid">对象ID（整柜为线路ID）</param>
        /// <param name="actid">动作ID</param>
        /// <param name="actions">动作名称</param>
        /// <param name="acotent">动作内容</param>
        /// <param name="searchcode">编号（整柜指线路编号）</param>
        /// <returns>日志ID</returns>
        public int CreateUserVisitLog(int uid, string lname, string visitip, int stype, string dportkey, string lportkey, string routeline, string boatcorp, int oid, int actid, string actions, string acotent, string searchcode)
        {
            DbParameter[] parms = {
					                DbHelper.MakeInParam("@uid", (DbType)SqlDbType.Int,4,uid),
					                DbHelper.MakeInParam("@lname", (DbType)SqlDbType.VarChar,20,lname),
					                DbHelper.MakeInParam("@visitip", (DbType)SqlDbType.VarChar,15,visitip),
					                DbHelper.MakeInParam("@stype", (DbType)SqlDbType.Int,4,stype),
					                DbHelper.MakeInParam("@dport_key", (DbType)SqlDbType.VarChar,50,dportkey),
					                DbHelper.MakeInParam("@lport_key", (DbType)SqlDbType.VarChar,50,lportkey),
					                DbHelper.MakeInParam("@routeline", (DbType)SqlDbType.Char,40,routeline),
					                DbHelper.MakeInParam("@boatcorp", (DbType)SqlDbType.Char,40,boatcorp),
					                DbHelper.MakeInParam("@oid", (DbType)SqlDbType.Int,4,oid),
					                DbHelper.MakeInParam("@actid", (DbType)SqlDbType.Int,4,actid),
					                DbHelper.MakeInParam("@actions", (DbType)SqlDbType.NVarChar,100,actions),
					                DbHelper.MakeInParam("@others", (DbType)SqlDbType.NVarChar,200,acotent),
					                DbHelper.MakeInParam("@searchcode", (DbType)SqlDbType.Char,40,searchcode)
                                  };
            return TypeConverter.ObjectToInt(DbHelper.ExecuteScalar(CommandType.StoredProcedure, string.Format("{0}uservisitlog_ADD", BaseConfigs.GetTablePrefix), parms), -1);
        }
        /// <summary>
        /// 根据条件获取用户搜索日志集合
        /// </summary>
        public int GetAllUserVisitLogCount(string querystring)
        {
            DbParameter[] parms = { DbHelper.MakeInParam("@conditions", (DbType)SqlDbType.VarChar, 2000, querystring) };
            return TypeConverter.ObjectToInt(DbHelper.ExecuteScalar(CommandType.StoredProcedure, string.Format("{0}getuservisitlogcountbycondition", BaseConfigs.GetTablePrefix), parms));
        }
        /// <summary>
        /// 获取全部用户搜索日志分页操作
        /// </summary>
        public DataTable GetAllUserVisitLogsByPage(int pageindex, int pagesize, string condition, string ordercolumn, string ordertype)
        {
            DbParameter[] parms = {
                          DbHelper.MakeInParam("@pageindex",(DbType)SqlDbType.Int,4,pageindex),
                          DbHelper.MakeInParam("@pagesize",(DbType)SqlDbType.Int,4,pagesize),
                          DbHelper.MakeInParam("@conditions",(DbType)SqlDbType.NVarChar,2000,condition),
                          DbHelper.MakeInParam("@ordercolumn",(DbType)SqlDbType.VarChar,20,ordercolumn),
                          DbHelper.MakeInParam("@ordertype",(DbType)SqlDbType.VarChar,5,ordertype)
            };
            return DbHelper.ExecuteDataset(CommandType.StoredProcedure, string.Format("{0}getuservisitlogbypage", BaseConfigs.GetTablePrefix), parms).Tables[0];
        }
        /// <summary>
        /// 删除用户搜索操作日志
        /// </summary>
        public void DelUserVisitLogs(string ids)
        {
            string commandText = string.Format("DELETE [{0}uservisitlog] WHERE [id] IN ({1})", BaseConfigs.GetTablePrefix, ids);
            DbHelper.ExecuteNonQuery(CommandType.Text, commandText);
        }
        #endregion

        #region 静态统计信息
        /// <summary>
        /// 根据企业ID获取企业统计信息
        /// </summary>
        public IDataReader GetStaticInfoByCid(int cid)
        {
            DbParameter[] parms = { DbHelper.MakeInParam("@cid", (DbType)SqlDbType.Int, 4, cid) };
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, string.Format("{0}getstaticbycid", BaseConfigs.GetTablePrefix), parms);
        }
        #endregion
    }
}
